<template>
  <div>
    <van-cell-group border>
      <van-field
        v-model="tel"
        label="手机号"
        placeholder="请输入手机号"
        autofocus
      />
    </van-cell-group>
    <div class="nextbtn">
      <van-button type="danger" round block :disabled="flag" @click="next"
        >下一步</van-button
      >
    </div>
  </div>
</template>

<script>
import Vue from 'vue';
import { Dialog } from 'vant';
Vue.use(Dialog);
import { docheckphone } from "../../api/user";
export default {
  data() {
    return {
      tel: "",
    };
  },
  computed: {
    flag() {
      //flag需要依赖tel来计算它的true和false
      //满足输入的内容 手机号的规则就是true
      if (/^1[3456789]\d{9}$/.test(this.tel)) {
        return false; //显示红色可用
      } else {
        return true; //显示灰红色 不可用
      }
    },
  },
  methods: {
    next() {
      //验证你的tel是否可以注册，连接接口
      docheckphone({ tel: this.tel }).then((res) => {
        console.log(res.data.code);
        switch (res.data.code) {
          case '10005':
            Dialog.confirm({
              message: "用户名已存在是否去登录",
            })
              .then(() => {
                this.$router.back()
              })
              .catch(() => {
                console.log("点了取消");
              });
            break;
          case '200':
            localStorage.setItem("tel",this.tel)
            this.$router.push("/register/two");
            break;
          default:
            break;
        }
      });
    },
  },
};
</script>

<style scoped>
.nextbtn {
  width: 80%;
  margin: 0.1rem auto;
}
</style>